# -*- coding: utf-8 -*-
# =============================================================================
#     FileName:
#         Desc:
#       Author:
#        Email:
#     HomePage:
#      Version:
#   LastChange:
#      History:
# =============================================================================
import io
import logging
import os.path
import sys
import argparse
from utils.logger_helper import LoggerHelper
from utils.big_key_parser import BigKeyParser

from utils.common_config import CommonConfig

logger = logging.getLogger(name=CommonConfig.app_name)


class CommandParser(object):

    @classmethod
    def get_parser(cls):
        parser = argparse.ArgumentParser(
            description='格式化MySQL慢日志并汇总SQL执行情况',
            add_help=False)
        connect_setting = parser.add_argument_group('参数信息')
        parser.add_argument(
            '--help',
            dest='help',
            action='store_true',
            help='获取帮助信息',
            default=False
        )
        connect_setting.add_argument(
            '--rdb_file_path',
            dest='rdb_file_path',
            type=str,
            default="",
            help='RDB文件地址,无默认值'
        )
        connect_setting.add_argument(
            '--top_key_count',
            dest='top_key_count',
            type=int,
            default=100,
            help="要返回的大Key的数量,默认值为100"
        )
        connect_setting.add_argument(
            '--top_key_type',
            dest='top_key_type',
            type=str,
            default="all",
            help="要返回的大Key的类型,默认为all(返回所有类型)"
        )
        connect_setting.add_argument(
            '--min_key_size',
            dest='min_key_size',
            type=int,
            default=1024 * 1024,
            help="最小key长度(单位Byte)，小于该值的key会被忽略,默认1M(1024*1024)"
        )
        # connect_setting.add_argument(
        #     '--scan_key_count',
        #     dest='scan_key_count',
        #     type=int,
        #     default=0,
        #     help="扫描KEY的数量，默认值0(扫描全部),当扫描KEY数量大于0时，仅扫描指定数量的key便停止扫描"
        # )
        return parser

    @classmethod
    def parse_args(cls, command_args):
        need_print_help = False if command_args else True
        parser = cls.get_parser()
        args = parser.parse_args(command_args)

        if args.help or need_print_help:
            parser.print_help()
            sys.exit(1)
        if not os.path.exists(args.rdb_file_path):
            logger.warning("RDB文件({}}不存在,请检查.".format(args.rdb_file_path))
            sys.exit(1)
        return args


def main(command_args):
    io.DEFAULT_BUFFER_SIZE = 16 * 1024 * 1024
    logger.info("处理开始。。。")
    args = CommandParser.parse_args(command_args)
    BigKeyParser.parse_rdb(
        rdb_file_path=args.rdb_file_path,
        top_key_count=args.top_key_count,
        top_key_type=args.top_key_type,
        min_key_size=args.min_key_size
    )
    logger.info("处理完成。。。")


if __name__ == '__main__':
    LoggerHelper.init_logger(logger_name="", logger_level=logging.INFO)
    main(sys.argv[1:])
